/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.subareas.settings;

import javax.swing.JFileChooser;
import javax.swing.JTextField;

import softstarter.filehandling.FileCentre;
import softstarter.filehandling.FolderFilter;
import softstarter.filehandling.indexer.FileIndexer;
import softstarter.launching.RunnableItem;


public class SettingsViewController {

    private SettingsView view;
    private FileIndexer indexer;
    
    public SettingsViewController(SettingsView view) {
        this.view = view;
        indexer = new FileIndexer();
    }
    
    public void saveChangesToFiles() {
        for(Object[] obj : view.getFilesAndButtons().values()) {
            String newName = ((JTextField)obj[0]).getText();
            RunnableItem item = (RunnableItem)obj[1];
            if(! newName.equals(item.getName())) {
                FileCentre.getInstance().renameFile(item, newName);
            }
        }
        view.buildListWithFilesAndRemoveButtons();
    }

    public void removeFile(Object source) {
        FileCentre.getInstance().removeFile((RunnableItem)view.getFilesAndButtons().get(source)[1]);
        view.buildListWithFilesAndRemoveButtons();
    }
    
    public void removeOldFiles() {
    	FileCentre.getInstance().removeOldFilesFromList();
    	view.buildListWithFilesAndRemoveButtons();
    }
    
    public void removeAllFiles() {
    	FileCentre.getInstance().removeAllFiles();
    	view.buildListWithFilesAndRemoveButtons();
    }

	public void indexAFolder() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new FolderFilter());
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int returnVal = fileChooser.showOpenDialog(view.getView());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            FileCentre.getInstance().addJavaFiles(indexer.getAllFilesInFolder(fileChooser.getSelectedFile()));
            view.buildListWithFilesAndRemoveButtons();
        } else {
        	System.out.println("Cancelled");
        }
	}

}
